package zcj.stu;

import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistrarBeanPostProcessor;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.view.BeanNameViewResolver;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Configuration
@PropertySource("classpath:application.properties")
public class WebConfig {
    //利用内嵌web容器工厂创建web容器
    @Bean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory(){
        return new TomcatServletWebServerFactory(8888);
    }
    //创建dispatchServlet
    @Bean
    public DispatcherServlet dispatcherServlet(){
        return new DispatcherServlet();
    }
    //将dispatchServlet注册到web容器中
    @Bean
    public DispatcherServletRegistrationBean registrationBean(DispatcherServlet dispatcherServlet){
        DispatcherServletRegistrationBean registrationBean =
                new DispatcherServletRegistrationBean(dispatcherServlet, "/");
        registrationBean.setLoadOnStartup(1);
        return registrationBean;//所有路径
    }
    @Bean
    public RequestMappingHandlerMapping mapping(){
        return   new RequestMappingHandlerMapping();
    }
    @Bean
    public RequestMappingHandlerAdapter adapter(){
        RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
        adapter.setMessageConverters(List.of(new MappingJackson2HttpMessageConverter()));
        return adapter;
    }
    @Bean
    public ErrorPageRegistrar errorPageRegistrar(){
          return registry -> registry.addErrorPages(new ErrorPage("/error"));
    }
    @Bean
    public ErrorPageRegistrarBeanPostProcessor errorPageProcessor(){
           return new ErrorPageRegistrarBeanPostProcessor();
    }
    @Controller
    static class MyController{
        @RequestMapping("foo")
        public void foo(){
            int i=100/0;
        }

//        @RequestMapping("error")
//        @ResponseBody
//        public String error(HttpServletRequest request, HttpServletResponse response){
//           Throwable throwable =(Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
//           return "<h1>出错了"+throwable.getMessage()+"</h1>";
//        }
    }
    @Bean
    public BasicErrorController errorController(){
        ErrorProperties errorProperties = new ErrorProperties();
        errorProperties.setIncludeException(true);
        return new BasicErrorController(new DefaultErrorAttributes(),errorProperties);
    }
    @Bean
    public View error(){
        return (model, request, response) -> {
            System.out.println(model);
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().print("<h1>出错了.,."+model+"</h1>");
        };
    }
    @Bean
    public ViewResolver viewResolver(){
        return new BeanNameViewResolver();
    }
}
